﻿Imports System.Collections.ObjectModel

Public Class ObjectEditorVm
    Inherits ViewModelBase

    Public Sub New()
        Definition = New DefinitionViewerVm

        mMoveTokenUpCommand = New Command(AddressOf MoveSelectedTokensUp)
        mMoveTokenDownCommand = New Command(AddressOf MoveSelectedTokensDown)
        mDeleteSelectedTokensCommand = New Command(AddressOf DeleteSelectedTokens)
        mInsertTokenCommand = New Command(AddressOf InsertToken)
    End Sub

#Region "Commands"
    Private mMoveTokenUpCommand As ICommand
    Public ReadOnly Property MoveTokenUpCommand As ICommand
        Get
            Return mMoveTokenUpCommand
        End Get
    End Property

    Private mMoveTokenDownCommand As ICommand
    Public ReadOnly Property MoveTokenDownCommand As ICommand
        Get
            Return mMoveTokenDownCommand
        End Get
    End Property

    Private mDeleteSelectedTokensCommand As ICommand
    Public ReadOnly Property DeleteSelectedTokensCommand As ICommand
        Get
            Return mDeleteSelectedTokensCommand
        End Get
    End Property

    Private mInsertTokenCommand As ICommand
    Public ReadOnly Property InsertTokenCommand As ICommand
        Get
            Return mInsertTokenCommand
        End Get
    End Property
#End Region

    Public Sub MoveSelectedTokensUp(param As Object)
        'Dim selectedToken As TokenBaseVm = mSelectedToken
        Dim tokens As IList(Of TokenBaseVm) = mCurrentObjectVM.Tokens
        If Not tokens.First.IsSelected Then
            For i As Integer = 1 To tokens.Count - 1
                Dim tokenVm As TokenBaseVm = tokens(i)
                If tokenVm.IsSelected Then
                    tokens.RemoveAt(i)
                    tokens.Insert(i - 1, tokenVm)

                    mCurrentObjectVM.RawObject.Tokens.RemoveAt(i)
                    mCurrentObjectVM.RawObject.Tokens.Insert(i - 1, tokenVm.Token)
                End If
            Next
            CurrentObjectVM.NotifyObjectChanged(True)
        End If
    End Sub

    Public Sub MoveSelectedTokensDown(param As Object)
        ' Dim selectedToken As TokenBaseVm = mSelectedToken
        Dim tokens As IList(Of TokenBaseVm) = mCurrentObjectVM.Tokens
        If Not tokens.Last.IsSelected Then
            For i As Integer = tokens.Count - 2 To 0 Step -1
                Dim tokenVm As TokenBaseVm = tokens(i)
                If tokenVm.IsSelected Then
                    tokens.RemoveAt(i)
                    tokens.Insert(i + 1, tokenVm)

                    mCurrentObjectVM.RawObject.Tokens.RemoveAt(i)
                    mCurrentObjectVM.RawObject.Tokens.Insert(i + 1, tokenVm.Token)
                End If
            Next
            CurrentObjectVM.NotifyObjectChanged(True)
        End If
    End Sub

    Public Sub DeleteSelectedTokens(param As Object)
        Dim tokens As IList(Of TokenBaseVm) = mCurrentObjectVM.Tokens
        For i As Integer = tokens.Count - 1 To 0 Step -1
            Dim tokenVm As TokenBaseVm = tokens(i)
            If tokenVm.IsSelected Then
                mCurrentObjectVM.RemoveToken(i, False)
                tokenVm.IsSelected = False
            End If
        Next
        mSelectedToken = Nothing
        CurrentObjectVM.NotifyObjectChanged(True)
    End Sub


    Public Sub InsertToken(param As Object)
        Dim frm As New AddTokenView(Me.CurrentObject, 0)
        If frm.ShowDialog = True Then
            'Actually insert the token
            Dim token As New Token("NAME", ({"hamster"}))

            CurrentObjectVM.InsertToken(0, token, True)
        End If
    End Sub

    Public Property CurrentObject As RawObject
        Get
            Return mCurrentObjectVM.RawObject
        End Get
        Set(value As RawObject)
            If mCurrentObjectVM Is Nothing OrElse mCurrentObjectVM.RawObject IsNot value Then
                CurrentObjectVM = New RawObjectVm(value) 'ObjectVmManager.Instance.GetObjectVm(value)
                RaisePropertyChanged("CurrentObject")
                UpdateDefinition()
            End If
        End Set
    End Property

    Private mCurrentObjectVM As RawObjectVm
    Public Property CurrentObjectVM As RawObjectVm
        Get
            Return mCurrentObjectVM
        End Get
        Set(value As RawObjectVm)
            If mCurrentObjectVM IsNot value Then
                mCurrentObjectVM = value
                RaisePropertyChanged("CurrentObjectVM")
                RaisePropertyChanged("IsObjectLoaded")
            End If
        End Set
    End Property


    Private mSelectedToken As TokenBaseVm
    Public Property SelectedToken As TokenBaseVm
        Get
            Return mSelectedToken
        End Get
        Set(value As TokenBaseVm)
            If mSelectedToken IsNot value Then
                'If mSelectedToken IsNot Nothing Then
                '    mSelectedToken.IsSelected = False
                'End If
                mSelectedToken = value
                'If mSelectedToken IsNot Nothing Then
                '    mSelectedToken.IsSelected = True
                'End If
                RaisePropertyChanged("SelectedToken")
                UpdateDefinition()
            End If
        End Set
    End Property

    Public ReadOnly Property IsObjectLoaded As Boolean
        Get
            Return mCurrentObjectVM IsNot Nothing
        End Get
    End Property

    Private mDefinition As DefinitionViewerVm
    Public Property Definition As DefinitionViewerVm
        Get
            Return mDefinition
        End Get
        Private Set(value As DefinitionViewerVm)
            mDefinition = value
            RaisePropertyChanged("Definition")
        End Set
    End Property

    Public Property Library As RawLibrary

    Private Sub UpdateDefinition()
        If mSelectedToken IsNot Nothing AndAlso CurrentObject IsNot Nothing Then
            Definition.Update(mSelectedToken.Token, CurrentObject)
        End If
    End Sub

    Public Sub InsertToken(index As Integer, token As Token)

    End Sub




End Class
